package com.magicborrow.activity;


import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.google.gson.Gson;
import com.magicborrow.R;
import com.magicborrow.utils.UIHelper;
import com.magicborrow.utils.ViewPressedUtil;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by ghost on 15/3/30.
 */
public class BaseActivity extends AppCompatActivity {
    private boolean hasTitle = true;
    private boolean addImmEd = false;

    public void setHasTitle(boolean hasTitle) {
        this.hasTitle = hasTitle;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 友盟推送的统计
        PushAgent.getInstance(this).onAppStart();
        // MyApplication.getInstance().addToActivityList(this);
    }

    protected void setImmerseLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            int statusBarHeight = UIHelper.getStatusHeight(this);
            ViewGroup vg = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
            View v = new View(this);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
            v.setLayoutParams(params);
            v.setBackgroundColor(getResources().getColor(R.color.title_color));
            vg.addView(v, 0);
            addImmEd = true;
        }
    }


    public void onBackClick(View v) {
        onBackPressed();
    }

    //asdasds
    ///dfqewfa
    public void showLongMsg(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    public void showShortMsg(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public JSONObject mapToJson(HashMap<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Gson gson = new Gson();
        String s = gson.toJson(map);
        // s = URLEncoder.encode(entry.getValue(), Constants.CODE_TYPE)
        JSONObject js = null;
        try {
            js = new JSONObject(s);
        } catch (Exception e) {
            showLongMsg(e.getMessage());
        }
        Log.i("POST", js.toString());
        return js;
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (hasTitle && !addImmEd) {
            setImmerseLayout();
        }
        MobclickAgent.onResume(this);
        View v = findViewById(R.id.imb_back);
        if (v != null) {
            v.setOnTouchListener(ViewPressedUtil.TouchPress);
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
